import {
  RECEIVE_USER_INFO,
  RECEIVE_CONTACTS,
  RECEIVE_FRIEND_REQUEST_LIST,
  RECEIVE_CURRENTSESSION,
  UPDATE_SESSIONLIST
} from './mutations-type'
export default {
  [RECEIVE_USER_INFO] (state, {userInfo}) {
    state.userInfo = userInfo
  },
  [RECEIVE_CONTACTS] (state, {contacts}) {
    state.contacts = contacts
  },
  [RECEIVE_FRIEND_REQUEST_LIST] (state, {friendRequestList}) {
    state.friendRequestList = friendRequestList
  },
  [RECEIVE_CURRENTSESSION] (state, {currentSession, msg, count}) { // 修改或重置currentSession
    // 当有msg或count时，是修改，否则是重置currentSeesion
    if (msg || count) {
      if (msg) currentSession.messages.push(msg)
      if (count) currentSession.count += count
    } else {
      state.currentSession = currentSession
    }
  },
  clearCurrentSessionCount (state) { // 清空currentSession的未读消息
    state.currentSession.count = 0
  },
  [UPDATE_SESSIONLIST] (state, {currentSession}) { // 更新sessionList,将一个会话插入到最前端
    let _index = false
    state.sessionList.forEach((session, index) => {
      if (session.accepter.friendId === currentSession.accepter.friendId) {
        _index = index
      }
    })
    if (_index !== false) { // currentSession已存在，排到第一
      const currentSession = state.sessionList.splice(_index, 1)[0]
      state.sessionList.unshift(currentSession)
    } else { // 不存在，直接插到第一位
      state.sessionList.unshift(currentSession)
    }
  },
  updateNewFriendCount (state, {count}) { // 当有好友加你时，增加小红点数量
    state.newFriendCount += count
  },
  clearNewFriendCount (state) { // 清零小红点
    state.newFriendCount = 0
  }
}
